<template>
	<view class="select-item">
		<uni-popup ref="popupSelect" @maskClick="close">
			<view class="popup-wrapper">
				<view class="title-wrapper" v-if="title">
					<uni-icons type="closeempty" @click="close" size="24" color="#454545" class="icon"></uni-icons>
					<text>{{ title }}</text>
					<view class="btn" @click="save">
						保存
					</view>
				</view>
				<view class="content-wrapper">
					<view class="item" @click="select(item)" :class="{'current':selectItem.includes(item)}"
						v-for="(item,index) in list" :key="index">
						<text>{{ item }}</text>
						<image v-if="selectItem.includes(item)"
							src="https://cdpresources.hobon.cc/supplier-mini-program/static/checked-icon.png"
							class="icon" mode=""></image>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		props: {
			data: {
				type: Array,
				default: []
			},
			value: {
				type: Array,
				default: []
			},
			show: {
				type: Boolean,
				default: false
			},
			title: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				selectItem: []
			}
		},
		mounted() {

		},
		computed: {
			list() {
				return this.data;
			},
			showFlag() {
				return this.show;
			},
		},
		watch: {
			showFlag(newValue, olbValue) {
				if (newValue) {
					this.selectItem = [...this.value];
					this.$refs.popupSelect.open('bottom')
				} else {
					this.$refs.popupSelect.close()
				}

			}
		},
		methods: {
			close() {
				this.$emit('change')
			},
			save() {
				this.$emit('change', this.selectItem)
			},
			select(item) {
				if (this.selectItem.includes(item)) {
					this.selectItem = this.selectItem.filter(items => {
						return items !== item;
					})
				} else {
					this.selectItem.push(item)
				}
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.select-item {
		.popup-wrapper {
			background-color: #ffffff;
			border-radius: 20upx 20upx 0 0;
			min-height: 600upx;
			position: relative;
			bottom: calc(0upx - env(safe-area-inset-bottom));

			.title-wrapper {
				height: 98upx;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;
				font-size: 36upx;

				.icon {
					position: absolute;
					left: 32upx;
					font-weight: bold;
				}

				.btn {
					color: #0E932E;
					font-size: 32upx;
					position: absolute;
					right: 32upx;
				}
			}

			.content-wrapper {
				display: flex;
				flex-wrap: wrap;
				padding: 32upx;

				.item {
					position: relative;
					padding: 12upx 40upx;
					border-radius: 4upx;
					background-color: #f9f9f9;
					color: #343434;
					border: 2upx solid #cecece;
					margin-right: 30upx;
					margin-bottom: 30upx;

					&.current {
						background-color: #EEFCEB;
						color: #0E932E;
						border: 2upx solid #0E932E;
					}

					.icon {
						width: 28upx;
						height: 24upx;
						position: absolute;
						bottom: 0upx;
						right: 0upx;
					}
				}
			}
		}
	}
</style>